package com.wang.mandkey.activity;

import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.wang.mandkey.R;
import com.wang.mandkey.net.Connecter;
import com.wang.mandkey.net.ConnecterPool;
import com.wang.mandkey.net.INetCallBack;
import com.wang.mandkey.tool.MediaTool;
import com.wang.mandkey.tool.RemoteOperateImpl;
import com.wang.mandkey.tool.Tool;
import com.wang.mandkey.utils.ProjectEnvironment;

public class PptAssistantActivity extends BaseActivity implements INetCallBack {

	private Button mButtonPlayOrEscape;

	private Button mButtonPrevious;
	private Button mButtonNext;

	private Button mButtonUp;
	private Button mButtonDown;

	private RemoteOperateImpl mRemoteOperateImpl;
	private Connecter connector;
	private boolean isPlay = false;
	private Button mButtonCloseDocument;
	private Button mButtonOpenDocument;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setUpView();
		MediaTool.addLoadListener(this);
	}

	@Override
	void setUpView() {
		setContentView(R.layout.ppt_help);
		connector = ConnecterPool.getConnectorByKey(ConnecterPool.STRING_CKEY);
		if (null != connector) {
			mRemoteOperateImpl = new RemoteOperateImpl(connector, this);
			ProjectEnvironment.BOOLEAN_LOCK_KEYBOAED = true;
		}

		mButtonPlayOrEscape = (Button) findViewById(R.id.buttonPptPlay);
		mButtonPlayOrEscape.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (null == mRemoteOperateImpl) {
					gotoSettingActivity();
				} else {
					MediaTool.load(PptAssistantActivity.this, R.raw.open_close);
					if (isPlay) {// 停止状态
						isPlay = false;
						mRemoteOperateImpl
								.sendCommand(ProjectEnvironment.STRING_COMMAND_PPT_ESC);
						mButtonPlayOrEscape
								.setBackgroundResource(R.drawable.play);
					} else {// 播放状态
						isPlay = true;
						mRemoteOperateImpl
								.sendCommand(ProjectEnvironment.STRING_COMMAND_PPT_F5);
						mButtonPlayOrEscape
								.setBackgroundResource(R.drawable.pause);
					}

				}
			}
		});

		mButtonPrevious = (Button) findViewById(R.id.buttonPptPrevious);
		mButtonPrevious.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null == mRemoteOperateImpl) {
					gotoSettingActivity();
				} else {
					mRemoteOperateImpl
							.sendCommand(ProjectEnvironment.STRING_COMMAND_PPT_PRE);
					MediaTool.load(PptAssistantActivity.this, R.raw.ken_down);
				}
			}
		});

		mButtonNext = (Button) findViewById(R.id.buttonPptNext);
		mButtonNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null == mRemoteOperateImpl) {
					gotoSettingActivity();
				} else {
					mRemoteOperateImpl
							.sendCommand(ProjectEnvironment.STRING_COMMAND_PPT_NEX);
					MediaTool.load(PptAssistantActivity.this, R.raw.ken_down);
				}
			}
		});

		mButtonUp = (Button) findViewById(R.id.buttonUp);
		mButtonUp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null == mRemoteOperateImpl) {
					gotoSettingActivity();
				} else {
					mRemoteOperateImpl
							.sendCommand(ProjectEnvironment.STRING_COMMAND_PPT_UP);
					MediaTool.load(PptAssistantActivity.this, R.raw.ken_down);
				}
			}
		});

		mButtonDown = (Button) findViewById(R.id.buttonDown);
		mButtonDown.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null == mRemoteOperateImpl) {
					gotoSettingActivity();
				} else {
					mRemoteOperateImpl
							.sendCommand(ProjectEnvironment.STRING_COMMAND_PPT_DOWN);
					MediaTool.load(PptAssistantActivity.this, R.raw.ken_down);
				}
			}
		});
		mButtonOpenDocument = (Button) findViewById(R.id.buttonOpenDocument);
		mButtonOpenDocument.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mRemoteOperateImpl.sendCommand(ProjectEnvironment.STRING_COMMAND_ENTER);
				MediaTool.load(PptAssistantActivity.this, R.raw.open_close);
			}
		});

		mButtonCloseDocument = (Button) findViewById(R.id.buttonCloseDocument);
		mButtonCloseDocument.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mRemoteOperateImpl.sendCommand(ProjectEnvironment.STRING_COMMAND_CLOSE);
				MediaTool.load(PptAssistantActivity.this, R.raw.open_close);
			}
		});

	}

	@Override
	public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
		super.onLoadComplete(soundPool, sampleId, status);
		MediaTool.play(sampleId);
	}

	private void gotoSettingActivity() {
		Tool.startOtherActivity(this, SettingActivity.class);
	}

	@Override
	public void OnFinish() {

	}

	@Override
	public void OnIntercepted(String source) {
		Tool.doNetLost(this);
	}

	@Override
	public void OnStart() {

	}
}
